<?php $this->load->view('header');?>

<body class="childrenBody custom_body">
	<div class="custom_main">
		<fieldset class="layui-elem-field layui-field-title">
		  <legend>文章信息</legend>
		</fieldset>

		<form class="layui-form">
			<div class="layui-form-item">
				<label class="layui-form-label">封面图：</label>
				<div class="layui-upload">
				  <button type="button" class="layui-btn layui-btn-primary" id="test1">上传</button>
				  <div class="layui-upload-list" style="display: inline;">
				    <img class="layui-upload-img" id="demo1">
				    <p id="demoText" style="display: inline;color: #ccc">建议正方形图片，大小不超过2M。支持格式jpg、jpeg、gif、png</p>
				    <input type="hidden" name="theme_img" value="<?=$theme_img?>" />
				  </div>
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label custom_i"><i>*</i> 文章标题：</label>
				<div class="layui-input-inline">
					<input type="text" name="title" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input" value="<?=$title?>">
				</div> 
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label custom_i"><i>*</i> 文章类型：</label>
				<div class="layui-input-inline">
			    	<select name="type_id" id="type_id" lay-verify="required">
					  <option value="" selected>请选择类型</option>
				  	  <?php foreach ($type_list as $item):?>
					  <option value="<?=$item['id']?>" <?php if ($type_id == $item['id']):?> selected <?php endif;?>><?=$item['name']?></option>
					  <?php endforeach;?>
					</select> 
			    </div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">文章详情：</label>
			    <div class="layui-input-block">
			      <textarea placeholder="请输入简介" id="LAY_demo1" class="layui-textarea" style="display: none"><?=$content?></textarea>
			    </div>
			</div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<input type="hidden" name="article_id" value="<?=$article_id?>" />
					<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="addUser">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			    </div>
			</div>
		</form>
		
	</div>

	<script type="text/javascript">
		layui.config({
			version: true
		}).use(['jquery','layer', 'form', 'laytpl', 'upload', 'layedit'],function(){
			var form = layui.form,
				layer = parent.layer === undefined ? layui.layer : parent.layer,
				laypage = layui.laypage,
				laytpl = layui.laytpl,
				upload = layui.upload,
				layedit = layui.layedit,
				$ = layui.jquery;

			var image_upload = '<?=$image_upload_url?>';

			//上传图片
			var uploadInst = upload.render({
			    elem: '#test1'
			    ,url: image_upload
			    ,before: function(obj){
			      //预读本地文件示例，不支持ie8
			      obj.preview(function(index, file, result){
			        $('#demo1').attr('src', result); //图片链接（base64）
			        $("#demo1").css("width","100px");
			      });
			    }
			    ,done: function(res){
			      //如果上传失败
			      if(res.result_code > 0){
			        return layer.msg('上传失败');
			      } 
			      //上传成功
			      else{
			      	$("input[name='theme_img']").val(res.data[0].url);
			      }
			     
			    }
			    ,error: function(){
			      //演示失败状态，并实现重传
			      var demoText = $('#demoText');
			      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
			      demoText.find('.demo-reload').on('click', function(){
			        uploadInst.upload();
			      });
			    }
			  });

				//富文本图片上传
				layedit.set({
				  uploadImage: {
				    url: 'admin.php/Article/upload_detailed_image' //接口url
				    ,type: 'post' //默认post
				  }
				});

				//构建一个默认的编辑器
				 var index = layedit.build('LAY_demo1', {
					  tool: ['strong', 'italic', 'underline', '|', 'del', 'left', 'center', 'right', 'link', 'unlink', 'image']
				  });

				//提交表单
				form.on("submit(addUser)",function(data)
				{
					data.field['content'] = layedit.getContent(index); //详情简介

					$.post('admin.php/Article/insert_article', data.field, function(result) {
						if (result.status == 200) 
						{
							layer.msg(result.message, {icon: 1});
							location.reload();
						}
						else
						{
							layer.msg(result.message, {icon: 5});
						}

					},"json");

					return false;
				});

				form.render(); //更新全部

		});

	</script>

</body>
</html>